package com.springboot.nucleic.controller;

import com.springboot.nucleic.po.Receipt;
import com.springboot.nucleic.po.Resident;
import com.springboot.nucleic.response.ResponseData;
import com.springboot.nucleic.response.SuccessResponseData;
import com.springboot.nucleic.service.ReceiptService;
import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/receipt")
public class ReceiptController {

    @Autowired
    private ReceiptService receiptService;

    @RequestMapping("save")
    @ResponseBody
    public ResponseData saveReceipt(@RequestBody Receipt receipt, HttpServletRequest request){
        receipt.setCreatetime(new Date());
        Resident resident = (Resident) request.getSession().getAttribute("ad");
        receipt.setUserid(resident.getId());
        receiptService.save(receipt);
        return new SuccessResponseData();
    }

    @RequestMapping("deleteReceipt")
    @ResponseBody
    public ResponseData deleteReceipt(int id){
        receiptService.deleteReceipt(id);
        return new SuccessResponseData();
    }

    @RequestMapping("editReceipt")
    @ResponseBody
    public ResponseData editReceipt(@RequestBody Receipt receipt){
        receiptService.updateReceipt(receipt);
        return new SuccessResponseData();
    }
}
